package com.ysstech.activiti.controller;

import java.util.List;
import java.util.Map;

import com.ysstech.activiti.model.CommentEntity;
import com.ysstech.common.controller.BaseController;
import com.ysstech.activiti.service.ActivitiTaskService;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@Api(tags = "流程运行时api")
@RequestMapping(value = "/activitiTask")
public class ActivitiTaskController extends BaseController {
    @Autowired
    private ActivitiTaskService activitiTaskService;
    @Autowired
    private IdentityService identityService;


    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "activiti/task";
    }

    @RequestMapping(value = "/querApprovalFlow")
    @ResponseBody
    public RestResult querApprovalFlow() throws Exception {
        Map<String, Object> map = activitiTaskService.querApprovalFlow(redisCacheManager.getLoginUserInfo().getLoginId());
        return ResultGenerator.getSuccessResult("查询待审核流程定义成功", map);
    }

    /**
     * 获取配置的地址，跳转到审批的页面
     */
    @RequestMapping(value = "/queryTaskFormKeyByTaskId")
    @ResponseBody
    public RestResult queryTaskFormKeyByTaskId(String taskId) throws Exception {
        String url = activitiTaskService.queryTaskFormKeyByTaskId(taskId);
        return ResultGenerator.getSuccessResult("查询待审核流程定义成功", url);
    }

    /**
     * 审核流程
     *
     * @param taskId
     * @param message 批注信息
     * @param outcome 通过还是驳回
     * @param key     流程定义的key
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/approvalFlow", produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult approvalFlow(String taskId, String message, String outcome, String key) throws Exception {
        activitiTaskService.approvalFlow(redisCacheManager.getLoginUserInfo().getLoginId(), outcome, taskId, message);
        return ResultGenerator.getSuccessResult("审核流程定义成功");
    }


    /**
     * 查询历史审批记录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryCommentByTaskId")
    @ResponseBody
    public RestResult queryCommentByTaskId(String taskId) throws Exception {
        List<CommentEntity> list = activitiTaskService.queryCommentByTaskId(taskId);
        return ResultGenerator.getSuccessResult("查询历史审批记录成功！", list);
    }
}
